/******************************************************************************
 * @File name   :      MemberGiftDaoImpl.java
 *
 * @Author      :      GUDONG
 *
 * @Date        :      2015年8月25日
 *
 * @Copyright Notice: 
 * Copyright (c) 2014 Capgemini, Inc. All  Rights Reserved.
 * This software is published under the terms of the Capgemini Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * 2015年8月25日 下午6:08:52        GUDONG     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.packagegift.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.cap.bts.crmdp.dealer.coupon.domain.Ticket;
import com.cap.bts.crmdp.dealer.packagegift.dao.IMemberGiftDao;
import com.cap.bts.crmdp.dealer.packagegift.domain.MemberGiftInfo;
import com.cap.bts.crmdp.dealer.packagegift.dto.MermberGiftDto;
import com.cap.bts.crmdp.dealer.purchase.domain.MemberPackageInfo;
import com.cap.bts.crmdp.dealer.purchase.domain.PackageDefinition;
import com.cap.bts.framework.common.dao.callback.QueryJpaCallback;
import com.cap.bts.framework.common.dao.impl.BaseDaoImpl;
import com.cap.bts.framework.common.exception.AppException;

/**
 *
 */
@Repository("memberGiftDao")
public class MemberGiftDaoImpl extends BaseDaoImpl<MemberGiftInfo, Long> implements IMemberGiftDao{

    /**
     * @Date        :      2015年8月25日
     * @param clazz
     */
    @Inject
    public MemberGiftDaoImpl(@Value("com.cap.bts.crmdp.dealer.packagegift.domain.MemberGiftInfo") Class<MemberGiftInfo> clazz) {
        super(clazz);
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年8月25日
     * @see com.cap.bts.crmdp.dealer.packagegift.dao.IMemberGiftDao#getGiftByPackageDefId(java.lang.Long)
    **/
    @Override
    public List<MemberGiftInfo> getPackageGiftByPackageDefId(Long packageDefId) throws AppException {
        String hql = "from MemberGiftInfo giftInfo where giftInfo.giftType='PACKAGE' and giftInfo.packageId=:packageDefId";
        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("packageDefId", packageDefId);
        return this.getListByQueryWithMap(hql, paraMap);
    }
    
    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年8月25日
     * @see com.cap.bts.crmdp.dealer.packagegift.dao.IMemberGiftDao#getTicketGiftByPackageDefId(java.lang.Long)
    **/
    @Override
    public List<MemberGiftInfo> getTicketGiftByPackageDefId(Long packageDefId) throws AppException {
        String hql = "from MemberGiftInfo giftInfo where giftInfo.giftType!='PACKAGE' and giftInfo.packageId=:packageDefId";
        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("packageDefId", packageDefId);
        return this.getListByQueryWithMap(hql, paraMap);
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年8月25日
     * @see com.cap.bts.crmdp.dealer.packagegift.dao.IMemberGiftDao#getPackageInfoByMemberIdAndDealerId(java.lang.Long, java.lang.Long)
    **/
    @Override
    public List<MemberPackageInfo> getPackageInfoByMemberIdAndDealerId(Long dealerId, Long memberId)
        throws AppException {
        String hql = "from MemberPackageInfo packageInfo where packageInfo.activeDealerId="+dealerId
            + "and packageInfo.activeMemberId="+memberId;
        return this.getJpaTemplate().execute(new QueryJpaCallback<MemberPackageInfo>(hql));
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      2015年8月26日
     * @see com.cap.bts.crmdp.dealer.packagegift.dao.IMemberGiftDao#getMemberPackageInfoListOfGiftByGiftId(java.lang.Long)
    **/
    @Override
    public List<PackageDefinition> getMemberPackageInfoListOfGiftByGiftId(Long giftId)
        throws AppException {
        String hql = "from PackageDefinition packageDef where packageDef.id="+ giftId;
        return this.getJpaTemplate().execute(new QueryJpaCallback<PackageDefinition>(hql));
    }
    

    /**
     * 入会礼(套餐礼物)信息新增
     * @see com.cap.bts.crmdp.dealer.packagegift.dao.IMemberGiftDao#saveMermberGiftInfo(com.cap.bts.crmdp.dealer.packagegift.dto.MermberGiftDto)
     */
    @Override
    public boolean saveMermberGiftInfo(MermberGiftDto mermberGiftDto) throws AppException {
        MemberGiftInfo memberGiftInfo = new MemberGiftInfo();
        memberGiftInfo.setPackageId(mermberGiftDto.getPackageId());
        memberGiftInfo.setGiftType(mermberGiftDto.getGiftType());
        memberGiftInfo.setGiftId(mermberGiftDto.getGiftId());
        
        save(memberGiftInfo);
        return true;
    }

    /**
     * 根据套餐ID、gift_type、gift_id查询入会礼信息
     * @see com.cap.bts.crmdp.dealer.packagegift.dao.IMemberGiftDao#getMermberGiftInfo(com.cap.bts.crmdp.dealer.packagegift.dto.MermberGiftDto)
     */
    @Override
    public MemberGiftInfo getMermberGiftInfo(MermberGiftDto mermberGiftDto) throws AppException {
        String sqlString = "from MemberGiftInfo giftInfo where giftInfo.packageId=:packageId and giftInfo.giftType=:giftType and giftInfo.giftId=:giftId";

        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("packageId", mermberGiftDto.getPackageId());
        paraMap.put("giftType", mermberGiftDto.getGiftType());
        paraMap.put("giftId", mermberGiftDto.getGiftId());
        
        List<MemberGiftInfo> list = this.getListByQueryWithMap(sqlString, paraMap);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }
    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Sep 24, 2015
     * @see com.cap.bts.crmdp.dealer.packagegift.dao.IMemberGiftDao#getTicketsInfoListOfGiftByGiftId(java.lang.Long)
    **/
    @Override
    public List<Ticket> getTicketListOfGiftByGiftId(Long giftId) throws AppException {
        String hql = "from Ticket a where  a.id="+ giftId;
        return this.getJpaTemplate().execute(new QueryJpaCallback<Ticket>(hql));
    }

    @Override
    public List<MemberGiftInfo> getGiftByPackageDefId(Long packageDefId) throws AppException {
        String hql = "from MemberGiftInfo giftInfo where giftInfo.packageId=:packageDefId";
        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("packageDefId", packageDefId);
        return this.getListByQueryWithMap(hql, paraMap);
    }
    
    @Override
    public List<MemberPackageInfo> getPackageInfoByMemberIdAndDealerIdAndCarId(Long dealerId, Long memberId, Long carId)
        throws AppException {
        String hql = "from MemberPackageInfo packageInfo where packageInfo.activeDealerId="+dealerId
            + "and packageInfo.activeMemberId="+memberId + " and packageInfo.activeCarId="+carId;
        return this.getJpaTemplate().execute(new QueryJpaCallback<MemberPackageInfo>(hql));
    }
}
